__author__ = 'Muscle'
import mui.muievent
from mui.muievent import *

class MUIList(list, MUIEventDispatcher):
    '''A list extended from python built-in list.

    The only difference to python built-in list is that
    it will generate 'Change' event when the list changed.

    Note, don't forget close().

    events:
        - Change
    '''
    def __init__(self, *argv, **argd):
        list.__init__(self, *argv, **argd)
        MUIEventDispatcher.__init__(self)

    def append(self, o):
        list.append(self, o)
        self.dispatch_event('Change')

    def extend(self, o):
        list.extend(self, o)
        self.dispatch_event('Change')

    def insert(self, i, o):
        list.insert(self, i, o)
        self.dispatch_event('Change')

    def remove(self, o):
        list.remove(self, o)
        self.dispatch_event('Change')

    def clear(self):
        """Remove all elements from list

        This equals to:
            del the_list[:]

        Note, EventManager.clear() is masked by this method.
        """

        l = len(self)
        self.__delslice__(0, l)

    def pop(self):
        ret = list.pop(self)
        self.dispatch_event('Change')
        return ret

    def sort(self, *argv, **argd):
        list.sort(self, *argv, **argd)
        self.dispatch_event('Change')

    def reverse(self):
        list.reverse(self)
        self.dispatch_event('Change')

    def __iadd__(self, o):
        ret = list.__iadd__(self, o)
        self.dispatch_event('Change')
        return ret

    def __imul__(self, o):
        ret = list.__imul__(self, o)
        self.dispatch_event('Change')
        return ret

    def __setitem__(self, index, value):
        ret = list.__setitem__(self, index, value)
        self.dispatch_event('Change')
        return ret

    def __delitem__(self, index):
        ret = list.__delitem__(self, index)
        self.dispatch_event('Change')
        return ret

    def __setslice__(self, i, j, seq):
        ret = list.__setslice__(self, i, j, seq)
        self.dispatch_event('Change')
        return ret

    def __delslice__(self, i, j):
        ret = list.__delslice__(self, i, j)
        self.dispatch_event('Change')
        return ret

